package com.hdsx.restful.glzt.structure.ql.rest;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.apache.wink.common.annotations.Workspace;
import org.springframework.stereotype.Service;

import com.hdsx.restful.base.PreparedDataListener;
import com.hdsx.restful.glzt.roadline.lxxx.bean.Znode;
import com.hdsx.restful.glzt.structure.ql.bean.Ql;
import com.hdsx.restful.glzt.structure.ql.bean.StatisticalResult;
import com.hdsx.restful.glzt.structure.ql.service.QlServer;
import com.hdsx.restful.util.CheckEmpty;
import com.hdsx.restful.util.Dao;
import com.hdsx.restful.util.EasyUI;


@Workspace(workspaceTitle = "restful", collectionTitle = "ql") 
@Path("/ql")
@Service
public class QlRestService {
	@Resource(name = "qlServerImpl")
	private QlServer server;

	
	@GET
	@Path("/getListByLxbm")
	@Produces(MediaType.APPLICATION_JSON)
	public EasyUI<Ql> getListByLxbm(@QueryParam("lxbm") String lxbm,@QueryParam("page") int page,@QueryParam("rows") int rows){
		EasyUI<Ql> easyUI=new EasyUI<Ql>(page,rows);
		int count =server.queryQlCountByLxbm(lxbm);
		List<Ql> qlListByLxbm = server.getListByLxbm(lxbm,easyUI.getBegin(),easyUI.getEnd());
		easyUI.setTotal(count);
		easyUI.setRows(qlListByLxbm);
		return easyUI;
	}
	
	@GET
	@Path("/getListByXzqhbm")
	@Produces(MediaType.APPLICATION_JSON)
	public EasyUI<Ql> getListByXzqhbm(@QueryParam("xzqhbm") String xzqhbm,@QueryParam("page") int page,@QueryParam("rows") int rows){
		EasyUI<Ql> easyUI=new EasyUI<Ql>(page,rows);
		int count =server.queryQlCountXzqhbm(xzqhbm);
		List<Ql> qlListByXzqhbm = server.getListByXzqhbm(xzqhbm,easyUI.getBegin(),easyUI.getEnd());
		easyUI.setTotal(count);
		easyUI.setRows(qlListByXzqhbm);
		return easyUI;
	}
	
	/**
	 * http://192.168.0.137:8080/restful/rest/ql/list?lxbm=H5&xzqhbm=610923&qlmc=桥0中233左&page=0&rows=10
	 * @param lxbm
	 * @param xzqhbm
	 * @param qlmc
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception 
	 */
	@GET
	@Path("/list")
	@Produces(MediaType.APPLICATION_JSON)
	public EasyUI<Ql> getList(@QueryParam("lxbm") String lxbm,@QueryParam("xzqhbm")String xzqhbm,
			@QueryParam("sxxfx") String sxxfx,@QueryParam("qdzh") String qdzh_string,
			@QueryParam("zdzh") String zdzh_string,@QueryParam("name") String name,
			@QueryParam("page") int page,@QueryParam("rows") int rows) throws Exception{
		EasyUI<Ql> easyUI=new EasyUI<Ql>(page,rows);
		Double qdzh = null;
		Double zdzh = null;
		try{
			qdzh = Double.parseDouble(qdzh_string);
			zdzh = Double.parseDouble(zdzh_string);
		}catch(Exception e){
			System.out.println("qdzh和zdzh没有输入或格式错误！");qdzh=null;zdzh=null;
		}
		xzqhbm = CheckEmpty.subXzqhbm(xzqhbm);
		int count =server.queryCount(lxbm,xzqhbm,sxxfx,qdzh,zdzh,name);
		List<Ql> results = server.queryList(lxbm,xzqhbm,sxxfx,qdzh,zdzh,name,easyUI.getBegin(),easyUI.getEnd());
		easyUI.setTotal(count);
		easyUI.setRows(results);
		return easyUI;
	}
	
	
	@GET
	@Path("/getByQldm")
	@Produces(MediaType.APPLICATION_JSON)
	public Ql getByQlbm(@QueryParam("qldm") String qldm){
		Ql ql = server.getOne(qldm);
		return ql;
	}
	
	@GET
	@Path("/countQlkjfldmByXzqhbm")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Ql> countQlkjfldmByXzqhbm(@QueryParam("xzqhbm") String xzqhbm) {
		List<Ql> list = server.countQlkjfldmByXzqhbm(xzqhbm);
		String[] arr={"其它","小桥","中桥","大桥","特大桥"};
		Set<String> keys= new HashSet<String>();
		String Qlkjfldm ="";
		for (Ql ql : list) {
			Qlkjfldm  = ql.getQlkjfldm();
			keys.add(Qlkjfldm);
		}
		for (String key : arr) {
			if(!keys.contains(key))
			{
				Ql ql = new Ql();
				ql.setQlkjfldm(key);
				ql.setSum("0");
				list.add(ql);
			}
		
		}
		return list;
	}
	
	@GET
	@Path("/countQlkjfldmByLxbm")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Ql> countQlkjfldmByLxbm(@QueryParam("lxbm") String lxbm) {
		List<Ql> list = server.countQlkjfldmByLxbm(lxbm);
		String[] arr={"其它","小桥","中桥","大桥","特大桥"};
		String key = null;
		Set<String> keys= new HashSet<String>();

		for (Ql ql : list) {
			key = ql.getQlkjfldm();
			keys.add(key);
		}

		for (String keyy : arr) {
			if(!keys.contains(keyy))
			{
				Ql ql = new Ql();
				ql.setQlkjfldm(keyy);
				ql.setSum("0");
				list.add(ql);
			}
		
		}
		
		return list;
	}
	
	@GET
	@Path("/info")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Dao>  getShapeById(@QueryParam("id") String id) {
		return server.getShapeById(id);
	}
	
	@GET
	@Path("/tjjg")
	@Produces(MediaType.APPLICATION_JSON)
	public StatisticalResult getStatisticalResult(@QueryParam("szlx") String szlx,@QueryParam("xzqhbm") String xzqhbm,@QueryParam("type") String type){
		xzqhbm = CheckEmpty.subXzqhbm(xzqhbm);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("lxbm", szlx);
		map.put("xzqhbm", xzqhbm);
		map.put("type", type);
		StatisticalResult tjjg = server.countByParameter(map);
		return tjjg;
	}
	
	/**
	 * 路线桥梁
	 * @return
	 */
	@GET
	@Path("/qlNode")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Znode> tree(){
		List<Znode> qlNode = PreparedDataListener.getData("ql");
		return qlNode;
//		return server.qlNode();
	}
	
	@GET
	@Path("/detail")
	@Produces(MediaType.APPLICATION_JSON)
	public EasyUI<Ql> detail(@QueryParam("szlx") String szlx,@QueryParam("xzqhbm") String xzqhbm,
			@QueryParam("kjfl") String kjfl,@QueryParam("nxfl")String nxfl,@QueryParam("hzfl") String hzfl,
			@QueryParam("pdfl") String pdfl,@QueryParam("page") int page,@QueryParam("rows") int rows){
		xzqhbm = CheckEmpty.subXzqhbm(xzqhbm);
		EasyUI<Ql> easyUI = new EasyUI<Ql>(page, rows);
		Map<String, Object> map = easyUI.getParameter();
		map.put("lxbm", szlx);
		map.put("xzqhbm", xzqhbm);
		try{
			map.put("kjfl", Integer.parseInt(kjfl));
		}catch (Exception e){
			System.out.println("不是跨径分类！");
		}
		try{
			map.put("nxfl", Integer.parseInt(nxfl));
		}catch (Exception e){
			System.out.println("不是年限分类！");
		}
		try{
			map.put("hzfl", Integer.parseInt(hzfl));
		}catch (Exception e){
			System.out.println("不是荷载分类！");
		}
		try{
			map.put("pdfl", Integer.parseInt(pdfl));
		}catch (Exception e){
			System.out.println("不是等级评定分类！");
		}
		List<Ql> detail = server.detail(map);
		int total = server.countDetail(map);
		easyUI.setRows(detail);
		easyUI.setTotal(total);
		return easyUI;
	}
	
    @GET
	@Path("/all")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Ql> all(@QueryParam("lxbm") String lxbm,@QueryParam("xzqhbm")String xzqhbm,
			@QueryParam("sxxfx") String sxxfx,@QueryParam("qdzh") String qdzh_string,
			@QueryParam("zdzh") String zdzh_string,@QueryParam("name") String name) throws Exception{
    	name=new String(name.getBytes("ISO-8859-1"),"UTF-8");
		Double qdzh = null;
		Double zdzh = null;
		try{
			qdzh = Double.parseDouble(qdzh_string);
			zdzh = Double.parseDouble(zdzh_string);
		}catch(Exception e){
			System.out.println("qdzh和zdzh没有输入或格式错误！");qdzh=null;zdzh=null;
		}
		xzqhbm = CheckEmpty.subXzqhbm(xzqhbm);
		List<Ql> list = server.listAll(lxbm,xzqhbm,sxxfx,qdzh,zdzh,name);
		return list;
	}
	
	@GET
	@Path("/one")
	@Produces(MediaType.APPLICATION_JSON)
	public Ql one(@QueryParam("id")String id){
		Ql one = server.queryById(id);
		return one;
	}
	
	@GET
	@Path("/auto")
	@Produces(MediaType.APPLICATION_JSON)
	public List<String> pienext(@QueryParam("name")String name){
		return server.selectName(name);
	}
}
